<template>
  <!--列表-->
  <router-exact-view name="example">
    <div style="width: 100%" class="template-list-page-container">
      <my-table
        v-loading="loading"
        :columns="columns"
        :data-source="data"
        :pagination="pagination"
        :btn-options="btnOptions"
        title="典型案例列表"
        @page-change="handlePageChange"
      >
        <div slot="name" slot-scope="scope">
          <pdf-enter
            :src="scope.row.url"
            :words="scope.row.name"
          />
        </div>
        <div slot="table-btn">
          <el-button type="primary" @click="addFile">新增案例</el-button>
        </div>
      </my-table>
    </div>
    <el-dialog title="新增案例" :visible.sync="dialogFormVisible">
      <el-form :model="form">
        <el-form-item label="案例名称" :label-width="formLabelWidth">
          <el-input v-model="form.name" autocomplete="off" />
        </el-form-item>
        <el-form-item label="案例链接" :label-width="formLabelWidth">
          <el-input v-model="form.url" autocomplete="off" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitFile">确 定</el-button>
      </div>
    </el-dialog>
  </router-exact-view>
</template>

<script>
import { getFileList, saveFile, delFile, stickFile } from '@/api/operate'
import MyTable from '@/components/my-table/index'
import RouterExactView from '@/components/routerExactView'
import PdfEnter from '@/components/pdf-enter'

import { exampleColumns } from '../columns'

export default {
  components: { RouterExactView, MyTable, PdfEnter },
  data() {
    const that = this
    return {
      /* 弹窗相关 */
      dialogFormVisible: false,
      form: {
        name: '',
        file: []
      },
      formLabelWidth: '120px',
      /* 列表相关 */
      columns: exampleColumns,
      data: [],
      searchForm: {
      },
      loading: false,
      pagination: {
        current: 1,
        pageSize: 10,
        total: 0
      },
      btnOptions: [
        {
          name: '删除',
          handler: function(row) {
            that.deleteFile(row)
          }
        },
        {
          name: '置顶',
          v_if: row => row.stick === 0,
          handler: function(row) {
            that.setTopFile(row, '置顶')
          }
        },
        {
          name: '取消置顶',
          v_if: row => row.stick === 1,
          handler: function(row) {
            that.setTopFile(row, '取消置顶')
          }
        }
      ]
    }
  },
  mounted() {
    if (this.$route.name === 'example') {
      this.handlePageChange()
    }
  },
  methods: {
    async handlePageChange(current, size) {
      if (current) {
        this.pagination.current = current
      }
      if (size) {
        this.pagination.pageSize = size
      }
      this.loading = true
      try {
        const res = await getFileList({
          pageNum: this.pagination.current,
          pageSize: this.pagination.pageSize,
          type: 2
        })
        if (res && res.list) {
          this.data = res.list
          this.pagination = {
            current: res.pageNum,
            pageSize: res.pageSize,
            total: res.totalElements
          }
        }
        this.loading = false
      } catch (err) {
        this.loading = false
      }
    },
    resetSearchForm() {
      this.searchForm = {}
      this.handlePageChange(1)
    },
    addFile() {
      this.dialogFormVisible = true
      this.form = {}
    },
    async submitFile() {
      if (this.form.name && this.form.url) {
        const params = JSON.parse(JSON.stringify(this.form))
        try {
          const res = await saveFile({
            ...params,
            type: 2
          })
          if (res) {
            this.$message({
              type: 'success',
              message: '新增典型案例成功！'
            })
            this.dialogFormVisible = false
            this.handlePageChange(1)
          }
        } catch (err) {
          console.log(err)
        }
      } else {
        this.$message({
          type: 'warning',
          message: '请将表单信息填写完整'
        })
      }
    },
    async deleteFile(row) {
      const that = this
      this.$confirm('确定要删除该典型案例？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消'
      }).then(async() => {
        try {
          const res = await delFile({ id: row.id })
          if (res) {
            that.$message({
              message: `删除典型案例成功！`,
              type: 'success'
            })
            that.handlePageChange(that.pagination.current)
          }
        } catch (err) {
          console.log('接口出错')
        }
      }).catch(() => {
        console.log('取消')
      })
    },
    async setTopFile(row, word) {
      const that = this
      this.$confirm(`确定要${word}该典型案例？`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消'
      }).then(async() => {
        try {
          const res = await stickFile({ id: row.id })
          if (res) {
            that.$message({
              message: `${word}典型案例成功！`,
              type: 'success'
            })
            that.handlePageChange(1)
          }
        } catch (err) {
          console.log('接口出错')
        }
      }).catch(() => {
        console.log('取消')
      })
    }
  }
}
</script>
<style scoped lang="less">
  @import "../../common.css";
  /* 查询条件容器的样式 */
  .search-area {
    padding: 16px 16px 0;
    margin-bottom: 8px;
    background: #fff;
  }
  a {
    color: #409EFF;
  }
</style>
